package com.shux.usermanager.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.shux.common.action.BaseAction;
import com.shux.usermanager.biz.ILoginBiz;
import com.shux.utils.constants.Constant;
import com.shux.utils.other.Jurisdiction;
import com.shux.utils.page.PageData;
import com.shux.utils.string.StringUtil;

/**
 **********************************************
 *  描述：
 * Simba.Hua
 * 2017年5月25日
 **********************************************
**/
@Controller
@RequestMapping("/usermanager")
public class LoginAction extends BaseAction{
    private @Autowired ILoginBiz loginBiz;
    
    @RequestMapping("/login")
    public String login(){

        return "jsp/index/login";
    }
    
    @RequestMapping(value = "/toLogin", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object toLogin(HttpServletRequest request){
        return loginBiz.toLogin(request);
    }
    @RequestMapping("/main/{changeMenu}")
    public ModelAndView index(@PathVariable("changeMenu")  String changeMenu){
        ModelAndView mv = loginBiz.mainIndex(changeMenu,this.getRequest());
        return mv;
    }
    @RequestMapping("/toTab")
    public String tab(){
        return "jsp/index/tab";
    }
    
    /**
     * 进入首页后的默认页面
     * 
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/defaultPage")
    public ModelAndView defaultPage() throws Exception {
      ModelAndView mv = this.getModelAndView();
      PageData pd = new PageData();
      pd.put("userCount",10); // 系统用户数
      pd.put("appUserCount",20); // 会员数
      mv.addObject("pd", pd);
      mv.setViewName("jsp/index/default");
      return mv;
    }
    
    /**
     * 退出
     * @return
     */
    @RequestMapping(value="/logout")
    public ModelAndView logout(){
        String username = Jurisdiction.getUsername();
        Session session = Jurisdiction.getSession();
        session.removeAttribute(Constant.SESSION_USERROL);
        session.removeAttribute(StringUtil.join(username,Constant.SESSION_ROLE_RIGHTS));
        session.removeAttribute(Constant.SESSION_USERNAME);
        session.removeAttribute(StringUtil.join(username, Constant.SESSION_QX));
        session.removeAttribute(StringUtil.join(username,Constant.SESSION_allmenuList));
        session.removeAttribute(StringUtil.join(username,Constant.SESSION_menuList));
        session.removeAttribute("changeMenu");
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return new ModelAndView("redirect:/usermanager/login");
    }
}
